package com.play.ballen.mall.ui

import androidx.fragment.app.Fragment
import com.play.ballen.R
import com.play.ballen.databinding.FragmentMallBinding
import com.play.ballen.mall.entity.MallBallLevel
import com.play.ballen.me.dialog.BottomSheetDialogUtils
import com.yuven.appframework.adapter.BaseViewPagerAdapter
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.component.BaseMVVMFragment
import com.yuven.baselib.component.viewmodel.BaseViewModel.EmptyVM
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.utils.ViewUtils

class MallFragment : BaseMVVMFragment<EmptyVM, FragmentMallBinding>() {

    private var isLoadData = false

    private val fragmentOfficial by lazy {
        MallSaleFragment.newInstance(TYPE_MALL_OFFICIAL)
    }
    private val fragmentUser by lazy {
        MallSaleFragment.newInstance(TYPE_MALL_USER)
    }

    private val fragmentList by lazy {
        arrayListOf<Fragment>(fragmentOfficial, fragmentUser)
    }

    private lateinit var adapter: BaseViewPagerAdapter
    private var listLevel: List<MallBallLevel>? = null
    private var setLevelText = false

    private val titles = arrayListOf(
        ResourceUtil.getString(R.string.mall_tab_sale_official),
        ResourceUtil.getString(R.string.mall_tab_sale_user)
    )

    private var selectLevelId = ""

    private val levelDialogUtils by lazy {
        BottomSheetDialogUtils<MallBallLevel>(mContext)
    }

    companion object {
        val TAG: String = MallFragment::class.java.simpleName

        const val TYPE_MALL_OFFICIAL = "0"
        const val TYPE_MALL_USER = "1"

        fun newInstance(): MallFragment {
            return MallFragment()
        }
    }

    override fun initIntent() {}

    override fun layoutId(): Int {
        return R.layout.fragment_mall
    }

    override fun viewModelClass(): Class<EmptyVM> {
        return EmptyVM::class.java
    }

    override fun initView() {
        dataBinding.fakeStatusBar.layoutParams.height = ViewUtils.getStatusHeight()

        dataBinding.llLevel.setOnSingleClickListener {
            levelDialogUtils.show(listLevel)
        }

        dataBinding.imgRecord.setOnSingleClickListener {
            MallRecordActivity.start(mContext)
        }
    }

    override fun initData() {
        fragmentOfficial.apply {
            levelCallBack = {
                //首次才默认取第一个
                if (!setLevelText && !it.isNullOrEmpty()) {
                    dataBinding.tvLevel.text = it[0].getItemString()
                    setLevelText = true
                }

                listLevel = it
            }
            getSelectLevelId = {
                selectLevelId
            }
        }

        fragmentUser.apply {
            getSelectLevelId = {
                selectLevelId
            }
        }

        levelDialogUtils.apply {
            itemCallback = tag@{ select ->
                if (select.id == selectLevelId) {
                    return@tag
                }

                selectLevelId = select.id
                dataBinding.tvLevel.text = select.getItemString()

                fragmentOfficial.notifyLevelChange()
                fragmentUser.notifyLevelChange()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (!isLoadData) {
            adapter = BaseViewPagerAdapter(childFragmentManager, fragmentList, titles)
            dataBinding.viewPager.adapter = adapter
            dataBinding.viewPager.offscreenPageLimit = fragmentList.size
            dataBinding.tabLayout.setViewPager(dataBinding.viewPager)
            isLoadData = true
        }
    }

}